package com.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reggie.dto.SetmealDto;
import com.reggie.entity.Category;
import com.reggie.entity.Setmeal;
import com.reggie.entity.SetmealDish;
import com.reggie.entity.ShoppingCart;
import com.reggie.exception.CustomException;
import com.reggie.mapper.SetmealMapper;
import com.reggie.service.CategoryService;
import com.reggie.service.SetmealDishService;
import com.reggie.service.SetmealService;
import com.reggie.service.ShoppingCartService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Override
    public Page<SetmealDto> pageSetmeal(int page, int pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.like(StringUtils.isNotBlank(name), Setmeal::getName, name);
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        this.page(setmealPage, setmealLambdaQueryWrapper);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> setmealDtos = new ArrayList<>();
        Set<Long> categories = new HashSet<>();
        records.forEach(re -> categories.add(re.getCategoryId()));
        List<Category> categoryList = categoryService.listByIds(categories);
        records.forEach(record -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            categoryList.forEach(category -> {
                if (category.getId().equals(record.getCategoryId()))
                    setmealDto.setCategoryName(category.getName());
            });
            setmealDtos.add(setmealDto);
        });
        setmealDtoPage.setRecords(setmealDtos);
        return setmealDtoPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSetmeal(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        if (setmealDishes != null) {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
            setmealDishService.saveBatch(setmealDishes);
        }
    }

    @Override
    public SetmealDto findById(Long id) {
        Setmeal byId = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(byId, setmealDto);
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(id != null, SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(setmealDishLambdaQueryWrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSetmeal(SetmealDto setmealDto) {
        Setmeal setmeal = this.getById(setmealDto);
        this.updateById(setmealDto);
        //如果修改了套餐价格，则将购物车里的菜品价格也给修改了
        if (!setmeal.getPrice().equals(setmealDto.getPrice())) {
            LambdaUpdateWrapper<ShoppingCart> shoppingCartLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            shoppingCartLambdaUpdateWrapper.eq(ShoppingCart::getSetmealId, setmealDto.getId());
            shoppingCartLambdaUpdateWrapper.set(ShoppingCart::getAmount, setmealDto.getPrice().divide(BigDecimal.valueOf(100)));
            shoppingCartService.update(shoppingCartLambdaUpdateWrapper);
        }
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSetmeal(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId, ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus, 1);
        long count = this.count(setmealLambdaQueryWrapper);
        if (count > 0)
            throw new CustomException("套餐售卖中不能删除");
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids);
        this.removeBatchByIds(ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }
}
